from PIL import Image
import numpy as np
from PIL import Image
import os


def RemoveBlackBackground2 ( input_path: str , file_name: str , output_path: str ) :
    target_im = Image.open ( os.path.join ( input_path , file_name ) )
    target_im = target_im.convert ( 'RGBA' )
    target_img_array = np.array ( target_im )
    # 透明度
    target_img_Alpha = np.max ( target_img_array [ : , : , :3 ] , axis = 2 )
    # gamma变换
    target_img_dis = np.power ( (target_img_Alpha / 255) , 3 ) * 255
    target_img_array [ : , : , 3 ] = target_img_Alpha

    res_img = Image.fromarray ( target_img_array )
    if not file_name.endswith ( ".png" ) :
        res_img.save ( os.path.join ( output_path , file_name [ :-4 ] + ".png" ) )
    else :
        res_img.save ( os.path.join ( output_path , file_name ) )

# 示例用法
input_path = "D:/uuuuuuuu/"
file_name = "Ribbons0.png"
output_path = "D:/uuuuuuuu/ww"
RemoveBlackBackground2(input_path, file_name,output_path)